#include "complexcalculatewindow.h"
#include "ui_complexcalculatewindow.h"

ComplexCalculateWindow::ComplexCalculateWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::ComplexCalculateWindow)
{
    ui->setupUi(this);
    /*初始化成员变量*/
    //窗口切换按钮
    this->btn_calculator = ui->btn_calculator;
    this->btn_matrix = ui->btn_matrix;
    this->btn_set = ui->btn_set;
    //输入文本框
    this->lineEdit_a = ui->lineEdit_a;
    this->lineEdit_b = ui->lineEdit_b;
    this->lineEdit_c = ui->lineEdit_c;
    this->lineEdit_d = ui->lineEdit_d;
    //四则运算
    this->rbtn_add = ui->rbtn_add;
    this->rbtn_sub = ui->rbtn_sub;
    this->rbtn_mul = ui->rbtn_mul;
    this->rbtn_div = ui->rbtn_div;
    //计算结果
    this->textBrowser_result = ui->textBrowser_result;
    this->btn_get = ui->btn_get;
    /* 绑定单击事件 */
    //计算
    connect(btn_get, &QPushButton::clicked, [=]{
       QString a, b, c, d;
       a = lineEdit_a->text();
       b = lineEdit_b->text();
       c = lineEdit_c->text();
       d = lineEdit_d->text();
       //判断输入合法
       if(Util::isNumStr(a) && Util::isNumStr(b) && Util::isNumStr(c) && Util::isNumStr(d)){
           int a1 = a.toInt(), b1 = b.toInt(), c1 = c.toInt(), d1 = d.toInt();
           Complex complex1 = Complex(a1, b1);
           Complex complex2 = Complex(c1, d1);
           qDebug()<<complex1.toString()<<complex2.toString()<<endl;

           QString result;
           if(rbtn_add->isChecked()){
//               qDebug()<<"add"<<endl;
               complex = service.add(complex1, complex2);
               result = complex1.toString() + "+" + complex2.toString();
           } else if(rbtn_sub->isChecked()){
//               qDebug()<<"sub"<<endl;
               complex = service.subtract(complex1, complex2);
               result = complex1.toString() + "-" + complex2.toString();
           }else if(rbtn_mul->isChecked()){
//               qDebug()<<"mul"<<endl;
               complex = service.multiply(complex1, complex2);
               result = complex1.toString() + "*" + complex2.toString();
           }else{
//               qDebug()<<"div"<<endl;
               complex = service.divide(complex1, complex2);
               result = complex1.toString() + "/" + complex2.toString();
           }
           result = result + "=";
           result = result + complex.toString();
           textBrowser_result->setText(result);
       }else{
           textBrowser_result->setHtml("<h1 style='color:red'>ERROR</h1>");
       }

    });
    //切换
    connect(btn_calculator, &QPushButton::clicked, [=]{
        this->hide();
        //设置新窗口的位置为当前窗口的位置
        ApplicationManager::windows[0]->setGeometry(this->geometry());
        ApplicationManager::windows[0]->show();
    });
    connect(btn_matrix, &QPushButton::clicked, [=]{
        this->hide();
        //设置新窗口的位置为当前窗口的位置
        ApplicationManager::windows[2]->setGeometry(this->geometry());
        ApplicationManager::windows[2]->show();
    });
    connect(btn_set, &QPushButton::clicked, [=]{
        this->hide();
        //设置新窗口的位置为当前窗口的位置
        ApplicationManager::windows[3]->setGeometry(this->geometry());
        ApplicationManager::windows[3]->show();
    });
}

ComplexCalculateWindow::~ComplexCalculateWindow()
{
    delete ui;
}
